<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/03/14
// +----------------------------------------------------------------------

namespace app\video\home;
use think\Controller;
use think\facade\Log;

use util\Jpush;
use util\Alipush;
/**
 * 公共类
 * @package app\user\admin
 */
class Publics extends Controller
{
    public function test1(){
        //运行超时
        ini_set('max_execution_time', '0');
            
        //查询所有点赞数为0的视频
        $map = [];
        $map[] = ['support_times', '<', 10];
        $map[] = ['play_operation_status', '=', 1];
        $map[] = ['father_play_id', '=', 0];
        $list = model('video/VideoPlay') -> where($map) -> select();

        //查询出所有1449开头的用户
        $map = [];
        $map[] = ['mobile', 'between', [14490000001, 14499999999]];
        $user_list = model('user_center/UcMemberMobile', 'model\user') -> where($map) -> select()->toArray();
        foreach ($list as $key => $value) {
            $num = rand(20,50);
            $rand_keys = array_rand($user_list, $num);
            $play_id = $value['id'];
            $create_time = rand($value['create_time'], time());
            foreach ($rand_keys as $key1 => $value1) {
                $userid = $user_list[$value1]['uid'];
                $res[] = controller('Videologic', 'logic') -> add_support($userid, $play_id, $create_time);//点赞
                
            }
            //dump($num);dump($rand_keys);
        }
        dump($res);dump($list);
        /* $rel = controller('common/PushFactory', 'designpattern\push') -> setPush($type='');
        $result = $rel->setPushByAlias($alias=114, $title=2, $content=3);
        dump($result); */
        
    }
    public function test(){
        /* $page_size = input('page_size');
        $map = [];
        $map['play_type'] = 2;
        $map['play_operation_status'] = 1;
        $map['son_play_id'] = 0;
        $list = model('VideoPlay') -> where($map) -> paginate($page_size);dump($list);
        foreach ($list as $key => $value) {

            $videoId        = $value['video_id'];

            //更新视频表状态
            $map = [];
            $map['video_id'] = $videoId;
            $rel = model('VideoPlay') -> where($map) -> find();

            if($rel->play_type == 2 && $rel->son_play_id == 0){//表示长视频 触发 剪辑
                $main_title = $rel->main_title;
                $description = $rel -> description;
                $uid = $rel ->uid;
                $video_classid = $rel->video_classid;
                $is_original = $rel->is_original;
                $father_play_id = $rel->id;
                $clip_rel = controller('Vodlogic', 'logic')->vodClip($videoId, $main_title, $description);
                $video_id = $clip_rel['data']['MediaId'];
                
                $video = model('VideoPlay') -> addData($uid, $video_id, $main_title, $sub_title ='', $description, $play_type=1, $is_immerse=0, $is_rotate=0, $cover_url='', $file_size='', $video_classid, $class_tage='', $is_original, $father_play_id, $play_operation_status =3);
                
                //更新 父亲的 son_play_id
                $map = [];
                $map['video_id'] = $videoId;
                $map['son_play_id'] = 0;
                model('VideoPlay') -> where($map) -> setField('son_play_id', $video['data']);

                //复制长视频的标签 给 短视频一份
                $map = [];
                $map['play_id'] = $rel->id;
                $lable_list = model('VideoPlayLabelRelation') -> where($map) -> select();
                $data = [];
                foreach ($lable_list as $key => $value) {
                    $data[] =[
                        'play_id'  => $video['data'],
                        'label_id' => $value['label_id'],
                        'create_time' => time(),
                        'update_time' => time(),
                    ];
                }
                $res2 = model('VideoPlayLabelRelation') -> insertAll($data);

            }
                
        } */
        //$rel = controller('Vodlogic', 'logic')->vodClip($videoId ='3b765f6d300849e4ba9242d6f5b57633', $title='测试剪辑', $description='测试描述');
        dump($rel);
        /* $uid = 715;
        $push_content = '测试内容';
        $push_title = '测试标题';

        $push = new Jpush();
        $push_result = $push -> push(array('alias'=>array($uid)), $push_content ,array('ios','android'), $push_title, 86400, $m_type = 3, $m_txt = ''); */


        /* $VideoId = 'f78b77216c64458b9f7d7a261c7306b5';
        $videobase = controller('Vodlogic', 'logic')->getPlayInfo($VideoId, $timeout = 3600);//获取视频播放信息 */
        /* $videoid = 'f415a93577d5478db6c5e822d09a52b6';
        $templateid = '9c9ac2ee0abc9df8810c8fbac17e5837';
        $rel = controller('Vodlogic', 'logic') -> submitTranscodeJobs($videoid, $templateid);
        $videoid = 'f415a93577d5478db6c5e822d09a52b6';
        $templateid = 'b208769c7afcc970a56ac5d890b207fd';
        $rel = controller('Vodlogic', 'logic') -> submitTranscodeJobs($videoid, $templateid); */
        /* $map = [];
        $map[] = ['id','>', 10];
        $map[] = ['play_authorid','=', null];
        $list = model('video/VideoPlayComment') -> where($map) -> select();
        foreach ($list as $key => $value) {
            //视频作者id
            $play_authorid = model('VideoPlay') -> cache(60) -> find($value['play_id']) -> uid;
            $uid = mt_rand(130,429);
            $data = [];
            $data['play_authorid'] = $play_authorid;
            $data['uid'] = $uid;dump($value['id']);dump($play_authorid);
            $rel = model('video/VideoPlayComment') -> save($data, ['id' => $value['id']]);
            dump($rel);
        } */
        //dump($list);//dump(model('video/VideoPlayComment')->getLastSql(1));
    }

    /**
     * 回调地址
     * @author jason wang <120907037@qq.com>
     * @return mixed
     */
    public function video_callback(){
        //dump(strtotime('2020-03-25T06:45:25:598Z'));die;2020-03-25T05:55:30Z
        //file_put_contents('video1111.text', $_SERVER['CONTENT_TYPE']);
        // server.php
        //switch($_SERVER['CONTENT_TYPE']){
            //case 'application/json':
                // 这里通过输入端来获取数据
                $body = file_get_contents('php://input');
                //$body = '{"Status":"success","VideoId":"d1ac94872df04a5aa869d53d5f69dab7","SubType":"video","AuditStatus":"Normal","MediaId":"d1ac94872df04a5aa869d53d5f69dab7","EventType":"CreateAuditComplete","EventTime":"2020-05-14T03:10:04Z","CreationTime":"2020-05-14T03:10:04Z"}';
                //$body = '{"Status":"success","MediaId":"4ddf810c2fb34cb69f86cf3793bb5963","EventType":"ProduceMediaComplete","EventTime":"2020-05-16T01:01:06Z","ProjectId":"e89b5d901c4d4f71b86a42149478b5c3"}';
                file_put_contents('video.text', '成功：'.$body.PHP_EOL, FILE_APPEND);
                $rel_arr = json_decode($body, true);
                switch ($rel_arr['EventType']) {
                    case 'FileUploadComplete'://上传完成
                        $VideoId    = $rel_arr['VideoId'];
                        $FileUrl    = $rel_arr['FileUrl'];
                        $EventTime  = $rel_arr['EventTime'];
                        $Size       = $rel_arr['Size'];
                        //$rel = controller('VideoApilogic', 'logic') -> file_upload_complete($VideoId, $FileUrl, $EventTime, $Size);//dump($rel);
                        $rel = controller('VideoApilogic', 'logic') -> file_upload_complete_xin($VideoId, $FileUrl, $EventTime, $Size);//dump($rel);
                        if($rel['code'] == 200){
                            header("status: 200 success");
                        }else{
                            header("status: 500 error");
                        }
                        break;
                    case 'SnapshotComplete'://截图完成
                        //file_put_contents('video1111.text', '成功：'.$body.PHP_EOL, FILE_APPEND);
                        //file_put_contents('../../runtime/video.text', '成功：'.$body.PHP_EOL, FILE_APPEND);
                        break;
                    case 'CreateAuditComplete'://人工审核完成
                        $VideoId      = $rel_arr['MediaId'];
                        $EventTime    = $rel_arr['CreationTime'];
                        $Status       = $rel_arr['Status'];
                        $AuditStatus  = $rel_arr['AuditStatus'];
                        $Auditor      = isset($rel_arr['Auditor']) ? $rel_arr['Auditor'] : '';
                        $Reason       = isset($rel_arr['Reason']) ? $rel_arr['Reason'] : '';
                        $rel = controller('VideoApilogic', 'logic') -> create_audit_complete($VideoId, $EventTime, $Status, $AuditStatus, $Auditor, $Reason);//dump($rel);
                        if($rel['code'] == 200){
                            header("status: 200 success");
                        }else{
                            header("status: 500 error");
                        }
                        break;
                    case 'AIMediaAuditComplete'://智能审核完成
                        $VideoId    = $rel_arr['MediaId'];
                        $EventTime  = $rel_arr['CompleteTime'];
                        $Status     = $rel_arr['Status'];
                        $rel = controller('VideoApilogic', 'logic') -> ai_media_audit_complete($VideoId, $EventTime, $Status);
                        if($rel['code'] == 200){
                            header("status: 200 success");
                        }else{
                            header("status: 500 error");
                        }
                        break;
                    case 'DeleteMediaComplete'://媒体删除完成
                            # code...
                        break;
                    case 'TranscodeComplete': //转码完成
                        $VideoId      = $rel_arr['VideoId'];
                        $aliVideoInfo = datacache('VideoPlay') -> getAliVideoInfoByVideoid($VideoId);//缓存生成
                        break; 
                    case 'ProduceMediaComplete': //媒体合成完成
                        $VideoId      = $rel_arr['MediaId'];
                        $rel = controller('VideoApilogic', 'logic') -> produceMediaComplete($VideoId);
                        if($rel['code'] == 200){
                            header("status: 200 success");
                        }else{
                            header("status: 500 error");
                        }
                        break;    
                    default:
                        # code...
                        break;
                }
                
                //break;
        //}

    }
    
    /**
     * 缓存生成
     * @author jason wang <120907037@qq.com>
     * @return mixed
     */
    public function cache_create(){
        //运行超时
        ini_set('max_execution_time', '0');
        $map = [];
        $map['play_operation_status'] = 1;
        $list = model('VideoPlay') -> where($map) -> select();
        foreach ($list as $key => $value) {
            $aliVideoInfo[] = datacache('VideoPlay') -> getAliVideoInfoByVideoid($value['video_id']);//用户统计缓存
        }
        //dump($aliVideoInfo);
    }
    
	 /**
     * 同步作品数量
     * @author shiqiang
     * @return mixed
     */
	public function worktimes(){
		$tongbu = isset($_GET['tongbu']) ? $_GET['tongbu'] : 0;
		$data_list = model('user_center/UcMemberCount', 'model\user') -> field('uid, works_times')->order('id asc') -> select() -> toArray();
		$worktimes = 0;
		$videotimes = 0;
		$videocount = 0;
		foreach($data_list as $key => $value){
			$map = [
				['uid', '=', $value['uid']],
				['play_type', '=', 1],
				['play_operation_status', '=', 1],
				['father_play_id', '=', 0],
			];
			$shortCount = model('video/VideoPlay', 'model') -> where($map) -> count();
			$data_list[$key]['shortCount'] = $shortCount;
			
			$map = [
				['uid', '=', $value['uid']],
				['play_type', '=', 2],
				['play_operation_status', '=', 1]
			];
			$longCount = model('video/VideoPlay', 'model') -> where($map) -> count();
			$data_list[$key]['longCount'] = $longCount;
			
			if($tongbu == 1){
				$data['works_times'] = $shortCount + $longCount;
				model('user_center/UcMemberCount', 'model\user') -> where('uid', $value['uid']) -> update($data);
			}
			$worktimes += $value['works_times'];
			$videotimes += $shortCount + $longCount;
		}
		echo '当前Count表总数: ' . $worktimes . '<br/>';
		echo '当前审核完成视频总数: ' . $videotimes . '<br/>';
	}

    /**
     * 异步
     * dou
     */
    public function video_curl()
    {
        $data = request()->post();
        if(isset($data) && !empty($data)){
            $video_id = $data['video_id'];
            $file_name = $data['file_name'];
            trace($video_id, 'input_params');
            trace($file_name, 'input_params');
            controller('Vodlogic','logic')->testUploadLocalVideo($file_name, $video_id);
        }else{}
    }
}
